﻿using System;
using System.Runtime.CompilerServices;

namespace PoolKit
{
    internal class Pooled<TResource>
        where TResource : class
    {
        private readonly TResource _resource;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="resource"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public Pooled(TResource resource)
        {
            if (resource == null) throw new ArgumentNullException("resource");

            _resource = resource;
        }

        public TResource Resource
        {
            get { return _resource; }
        }

        public override bool Equals(object obj)
        {
            Pooled<TResource> objectTyped = obj as Pooled<TResource>;

            if (objectTyped == null)
            {
                return false;
            }

            return Object.ReferenceEquals(_resource, objectTyped.Resource);
        }

        public override int GetHashCode()
        {
            return RuntimeHelpers.GetHashCode(_resource);
        }

        public override string ToString()
        {
            return Resource.ToString();
        }
    }
}